<?php
/**
 * Created by IntelliJ IDEA.
 * User: luo
 * Date: 18-3-7
 * Time: 下午4:10
 */
namespace app\tools;

use phpDocumentor\Reflection\Types\Self_;
use yii\helpers\ArrayHelper;
use Yii;

class Re
{
    const OK        = 0;
    const ERROR     = 1; // 用户验证失败
    const NOT_FOUND = 404; // 未找到对应资源

    /**
     * @param null|array $data
     * @param string $text
     * @param null $code
     * @return array
     */
    public static function data($data = null, $text = '', $code = null){
        $code === null and $code = Re::OK;
        $re = [
            'code'  => $code,
            'text' => (string)$text
        ];
        if($data === null){
            return $re;
        }elseif(is_array($data)){
            return ArrayHelper::merge($re, $data);
        }else{
            $re['data'] = $data;
            return $re;
        }
    }

    public static function json($data = null, $text = '', $code = null){
        return self::data($data, $text, $code);
    }
}
